package com.xyz.apis.modules.api.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.concurrent.Future;

@Component
public class AsyncTaskDemo {

    protected Logger logger = LoggerFactory.getLogger(getClass());

    @Async
    public Future<String> doTaskA(String test){
        try {
            for (int i = 1; i <= 3; i++) {
                logger.info("模拟延时{}秒执行",i);
                Thread.sleep(1000);
            }
            logger.info("模拟完成....");
        }catch (Exception e){
            logger.error("error:{},{}",e,e.getMessage());
        }
        return new AsyncResult<>("任务一完成");
    }

    @Async
    public void doTaskB(String test){
        try {
            for (int i = 1; i <= 3; i++) {
                logger.info("模拟延时{}秒执行",i);
                Thread.sleep(1000);
            }
            logger.info("模拟完成....");
        }catch (Exception e){
            logger.error("error:{},{}",e,e.getMessage());
        }
    }
}
